﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using System.Threading;

namespace MediaCenter.Controls
{
    public class LCARS_Button : LCARS_Control
    {
        private ElboType _TextAligment;
        private Device D3X_Dev;
        private Microsoft.DirectX.Direct3D.Font dx_text;
        private bool _RoundRight;
        private bool _RoundLeft;
        private bool _isComplex;

        public bool RoundLeft
        {
            get
            {
                return _RoundLeft;
            }
            set
            {
                _RoundLeft = value;
            }
        }
        
        public bool IsComlex
        {
            get
            {
                return _isComplex;
            }
            set
            {
                _isComplex = value;
            }
        }

        public bool RoundRight
        {
            get
            {
                return _RoundRight;
            }
            set
            {
                _RoundRight = value;
            }
        }

        public ElboType TextAligment
        {
            get
            {
                return _TextAligment;
            }
            set
            {
                _TextAligment = value;
            }
        }

        public LCARS_Button()
        {
            _TextAligment = ElboType.oben_links;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.DesignMode || this.PartmentForm.RenderMode == RenderMode.GDI)
            {
                Rectangle rectangle = new Rectangle(0, 0, this.Width, this.Height);
                if (_RoundLeft)
                {
                    gdi_draw_Kreis(e.Graphics, 90, 270, 60, this.Height / 2, new Point(this.Height / 2, this.Height / 2));
                    rectangle.Width -= this.Height / 2;
                    rectangle.X += this.Height / 2;
                }
                
                if (_RoundRight)
                {
                    gdi_draw_Kreis(e.Graphics, 270, 450, 60, this.Height / 2, new Point(this.Width - this.Height / 2, this.Height / 2));
                    rectangle.Width -= this.Height / 2;
                }

                e.Graphics.FillRectangle(new SolidBrush(this.BackColor), rectangle);
                int txtbreite = TextRenderer.MeasureText(this.Text, this.Font).Width;
                int txthöhe = TextRenderer.MeasureText(this.Text, this.Font).Height;
                Brush strB = new SolidBrush(this.ForeColor);
                switch (_TextAligment)
                {
                    case ElboType.oben_links:
                        e.Graphics.DrawString(this.Text, this.Font, strB, rectangle.Left + 5, rectangle.Height + 5);
                        break;
                    case ElboType.oben_rechts:
                        e.Graphics.DrawString(this.Text, this.Font, strB, rectangle.Left + rectangle.Width - txtbreite, rectangle.Height + 5);
                        break;
                    case ElboType.unten_links:
                        e.Graphics.DrawString(this.Text, this.Font, strB, rectangle.Left + 5, rectangle.Height - txthöhe - 5);
                        break;
                    case ElboType.unten_rechts:
                        e.Graphics.DrawString(this.Text, this.Font, strB, rectangle.Left + rectangle.Width - txtbreite - 5, rectangle.Height - txthöhe - 5);
                        break;
                }
            }
        }


        private void gdi_draw_Kreis(Graphics g, int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            Brush b = new SolidBrush(this.BackColor);
            double grad = System.Convert.ToDouble(startgrad);
            double addgrad_t = (endgrad - startgrad) / teile * (1 - 1);
            double angle_t = Math.PI * (grad + (addgrad_t)) / 180.0;
            double sinus_t = Math.Sin(angle_t) * -1;
            double cosinus_t = Math.Cos(angle_t);
            int x_t = System.Convert.ToInt32(Possition.X + radius * cosinus_t);
            int y_t = System.Convert.ToInt32(Possition.Y + radius * sinus_t);

            Point lastpoint = new Point(x_t, y_t);

            for (int i = 2; i < teile + 2; i++)
            {
                int pos = teile + 2 - i;
                //int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i - 1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0;
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(Possition.X + radius * cosinus);
                int y = System.Convert.ToInt32(Possition.Y + radius * sinus);

                Point[] p = new Point[3];
                p[0] = Possition;
                p[1] = lastpoint;
                p[2] = new Point(x, y);
                g.FillPolygon(b, p);
                lastpoint = p[2];
            }
        }


        private void draw_Kreis(int startgrad, int endgrad, int teile, int radius, Point Possition)
        {
            CustomVertex.TransformedColored[] _3Eck = new CustomVertex.TransformedColored[teile + 2];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(Possition.X, Possition.Y, 0, 2.0f); ;
            _3Eck[0].Color = this.BackColor.ToArgb();

            double grad = System.Convert.ToDouble(startgrad);

            for (int i = 1; i < teile + 2; i++)
            {
                int pos = teile + 2 - i;
                //int pos = i;
                double addgrad = (endgrad - startgrad) / teile * (i - 1);
                double angle = Math.PI * (grad + (addgrad)) / 180.0;
                double sinus = Math.Sin(angle) * -1;
                double cosinus = Math.Cos(angle);
                int x = System.Convert.ToInt32(Possition.X + radius * cosinus);
                int y = System.Convert.ToInt32(Possition.Y + radius * sinus);
                _3Eck[pos].Position = new Microsoft.DirectX.Vector4(x, y, 0, 2.0f); ;
                _3Eck[pos].Color = this.BackColor.ToArgb();
            }

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleFan, teile, _3Eck);
        }


        private void draw_4Eck(Rectangle data)
        {

            CustomVertex.TransformedColored[] _3Eck = new CustomVertex.TransformedColored[3];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(data.X, data.Y, 0, 2.0f); ;
            _3Eck[1].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y, 0, 2.0f); ;
            _3Eck[2].Position = new Microsoft.DirectX.Vector4(data.X, data.Y + data.Height, 0, 2.0f); ;


            _3Eck[0].Color = this.BackColor.ToArgb();
            _3Eck[1].Color = this.BackColor.ToArgb();
            _3Eck[2].Color = this.BackColor.ToArgb();

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleList, 1, _3Eck);

            _3Eck = new CustomVertex.TransformedColored[3];
            _3Eck[0].Position = new Microsoft.DirectX.Vector4(data.X, data.Y + data.Height, 0, 2.0f); ;
            _3Eck[1].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y, 0, 2.0f); ;
            _3Eck[2].Position = new Microsoft.DirectX.Vector4(data.X + data.Width, data.Y + data.Height, 0, 2.0f); ;


            _3Eck[0].Color = this.BackColor.ToArgb();
            _3Eck[1].Color = this.BackColor.ToArgb();
            _3Eck[2].Color = this.BackColor.ToArgb();

            this.D3X_Dev.VertexFormat = CustomVertex.TransformedColored.Format;
            this.D3X_Dev.DrawUserPrimitives(PrimitiveType.TriangleList, 1, _3Eck);
        }

        public override void Render()
        {
            base.Render();
            if (!this.DesignMode)
            {
                D3X_Dev = this.PartmentForm.D3X_Dev;
                dx_text = new Microsoft.DirectX.Direct3D.Font(D3X_Dev, this.Font);

                draw_4Eck(new Rectangle(this.Left, this.Top, this.Width, this.Height));


                dx_text = null;
            }
        }
    }
}
